package huixue.cn.com.rxapp.rx;

import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.view.View;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import huixue.cn.com.rxapp.R;
import huixue.cn.com.rxapp.base.SimpleActivity;
import huixue.cn.com.rxapp.util.LogUtils;

public class ThreadPoolActivity extends SimpleActivity {

    @BindView(R.id.btn1)
    AppCompatButton btn1;

    @Override
    public int getLayout() {
        return R.layout.activity_thread_pool;
    }

    @Override
    public void initView() {

    }

    @Override
    public void addEvent() {

    }


    @OnClick({R.id.btn1})
    public void onViewClicked(View view) {
        if(view.getId()==R.id.btn1){
            thread1();

        }

    }

    /**
     *
     */
    private void thread1(){
        final ThreadPoolExecutor threadPoolExecutor
                = new ThreadPoolExecutor(3,5,1, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>(25));
        for(int i = 0;i<30;i++){
            final int finali = i;
            Runnable runnable = new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(2000);
                        LogUtils.d("Thread", "run: "+finali);
                        LogUtils.d("当前线程：",Thread.currentThread().getName());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };

            threadPoolExecutor.execute(runnable);
            LogUtils.d("Thread", "finali: "+finali);
        }
    }

    private  void thread2(){
        ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);

        //Executors.newCachedThreadPool()

    }

}
